package com.ruoyi.costBatch.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.cost.domain.FundCostT;
import com.ruoyi.costBatchDetail.domain.HouseBatchDetailT;
import com.ruoyi.cost.mapper.HouseCostTMapper;
import com.ruoyi.costBatchDetail.mapper.HouseBatchDetailTMapper;
import com.ruoyi.costQuery.domain.HouseCostT;
import com.ruoyi.data.DataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.costBatch.mapper.HouseBatchTMapper;
import com.ruoyi.costBatch.domain.HouseBatchT;
import com.ruoyi.costBatch.service.IHouseBatchTService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 批量缴费Service业务层处理
 * 
 * @author lijie
 * @date 2023-04-24
 */
@Service
public class HouseBatchTServiceImpl implements IHouseBatchTService 
{
    @Autowired
    private HouseBatchDetailTMapper houseBatchDetailTMapper;

    @Autowired
    private HouseCostTMapper houseCostTMapper;

    @Autowired
    private HouseBatchTMapper houseBatchTMapper;

    /**
     * 查询批量缴费
     * 
     * @param batchId 批量缴费主键
     * @return 批量缴费
     */
    @Override
    public HouseBatchT selectHouseBatchTByBatchId(Long batchId)
    {
        return houseBatchTMapper.selectHouseBatchTByBatchId(batchId);
    }

    /**
     * 查询批量缴费列表
     * 
     * @param houseBatchT 批量缴费
     * @return 批量缴费
     */
    @Override
    public List<HouseBatchT> selectHouseBatchTList(HouseBatchT houseBatchT)
    {
        return houseBatchTMapper.selectHouseBatchTList(houseBatchT);
    }

    /**
     * 新增批量缴费
     * 
     * @param houseBatchT 批量缴费
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHouseBatchT(HouseBatchT houseBatchT)
    {
        return houseBatchTMapper.insertHouseBatchT(houseBatchT);
    }

    /**
     * 修改批量缴费
     * 
     * @param houseBatchT 批量缴费
     * @return 结果
     */
    @Transactional
    @Override
    public int updateHouseBatchT(HouseBatchT houseBatchT)
    {
        return houseBatchTMapper.updateHouseBatchT(houseBatchT);
    }

    /**
     * 批量删除批量缴费
     * 
     * @param batchIds 需要删除的批量缴费主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHouseBatchTByBatchIds(Long[] batchIds)
    {
        return houseBatchTMapper.deleteHouseBatchTByBatchIds(batchIds);
    }

    /**
     * 删除批量缴费信息
     * 
     * @param batchId 批量缴费主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHouseBatchTByBatchId(Long batchId)
    {
        return houseBatchTMapper.deleteHouseBatchTByBatchId(batchId);
    }

    /**
     * 执行批量缴费信息
     *
     * @param houseBatchDetailT,batchIds 批量缴主键
     * @return 结果
     */
    @Transactional
    @Override
    public int updateExecuteCostBatch(HouseBatchDetailT houseBatchDetailT, Long[] batchIds, Date costDate)
    {
        Double sumMoney = 0d;
        int door_sum = 0;
        int result = 0;
        for(Long batchId:batchIds){
            HouseBatchT houseBatchT = new HouseBatchT();
            List<HouseBatchDetailT> batchDetaillist = new ArrayList<HouseBatchDetailT>();
            batchDetaillist = houseBatchDetailTMapper.selectBatchDetailTByBatchId(batchId);
            for(HouseBatchDetailT batchDetailT:batchDetaillist){
                FundCostT fundCostVO = new FundCostT();
                HouseCostT houseCostT = new HouseCostT();
                fundCostVO = houseCostTMapper.selectFundCostTByHouseId(batchDetailT.getHouseId());
                fundCostVO.setCreateDate(costDate);
                fundCostVO.setBalance(fundCostVO.getBalance()+fundCostVO.getCostMoney());
                fundCostVO.setBatchId(batchId);
                fundCostVO.setCostDate(costDate);
                int costId = houseCostTMapper.getCostId();
                houseCostT.setSerial(String.valueOf(costId+1));
                houseCostT.setCostDate(costDate);
                houseCostT.setCreater(houseBatchDetailT.getCreater());
                houseCostT.setHouseId(batchDetailT.getHouseId());
                houseCostT.setCostMoney(fundCostVO.getCostMoney());
                houseCostT.setBatchId(batchId);
                houseCostTMapper.insertHouseCostBatchT(houseCostT);
                houseCostTMapper.updateHouseDoorBatchT(fundCostVO);
                sumMoney = DataUtils.sumMoney(sumMoney,fundCostVO.getCostMoney());
                door_sum++;
            }
            houseBatchT.setMoneySum(sumMoney);
            houseBatchT.setBatchId(batchId);
            houseBatchT.setCostDate(costDate);
            houseBatchT.setDoorSum(Long.parseLong(String.valueOf(door_sum)));
            houseBatchT.setBatchSts("1");
            result = houseBatchTMapper.updateHouseBatchT(houseBatchT);
        }
        return result;
    }
}
